﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO;

public class XMLParser
{
    private string file;

    public XMLParser()
    {
        this.file = @"C:\AuthentificatedIPs.xml";
    }

    public void WriteClientIP(string IP)
    {
        XElement doc = XElement.Load(file);

        XElement newClient = new XElement("client");
        newClient.SetAttributeValue("name", IP);

        doc.Add(newClient);
        doc.Save(file);
    }

    public void DeleteClientIP(string IP)
    {
        XElement doc = XElement.Load(file);

        var elements = from firstLevel in doc.Elements()
                       where (firstLevel.Attribute("name").Value == IP)
                       select firstLevel;

        elements.First().Remove();

        doc.Save(file);
    }

    public bool ClientExist(string IP)
    {
        XElement doc = XElement.Load(file);

        var elements = from firstLevel in doc.Elements()
                       where (firstLevel.Attribute("name").Value == IP)
                       select firstLevel;

        if (elements.Count() != 0)
            return true;
        else
            return false;
    }
}

